<?php

class FP_Application_Subsystem_Logger implements FP_Application_Subsystem_Interface {

    public static $enabled = false;
    public static $logger = null;

    protected $_config = null;

    public function init($pConfig=null){

	$this->_config = $pConfig;

	self::$enabled = $this->_config->enabled;

	if (self::$enabled){

	    $logger = new Zend_Log(new Zend_Log_Writer_Stream(BASE_DIR . 'log/' . strtolower(FP_Application::GetStagingEnvironment()) . '.log'));
	    $logfilter = new Zend_Log_Filter_Priority(intval($this->_config->priority));
	    $logger->addFilter($logfilter);

	} else {

	    $logger = new Zend_Log(new Zend_Log_Writer_Null());
	}

	self::$logger = $logger;
    }

}

function logobject($pObj, $pEcho=false){
  return Zend_Debug::dump($pObj, null, $pEcho);
}

function logtrace($pMessage, $traces_to_ignore = 1){
    $LOGGER = FP_Application_Subsystem_Logger::$logger;

    $traces = debug_backtrace();
    $ret = array();
    foreach($traces as $i => $call){
        if ($i < $traces_to_ignore ) {
            continue;
        }

        $object = '';
        $trace = '#'.str_pad($i - $traces_to_ignore, 3, ' ').$object.$call['function'] . '() called at [' . ((array_key_exists('file', $call)) ? $call['file'] : '') . ':' . ((array_key_exists('line', $call)) ? $call['line'] : '') . ']';
	$LOGGER->info($trace);
    }
}

function logerr($pSrc, $pMessage=null){
    if (FP_Application_Subsystem_Logger::$enabled){

	$LOGGER = FP_Application_Subsystem_Logger::$logger;

// 	$bt = debug_backtrace();
// 	$btindex = 1;
// 	if (array_key_exists($btindex, $bt)){
// 	    $lineno = isset($bt[$btindex]['line']) ? $bt[$btindex]['line'] : '';
// 	    $file = isset($bt[$btindex]['file']) ? $bt[$btindex]['file'] : '';
// 	    $function = isset($bt[$btindex]['function']) ? $bt[$btindex]['function'] : '';
// 	    $prefix = "$file:$function:$lineno:";
// 	} else
	    $prefix = '';

	$message = (is_null($pMessage)) ? $pSrc : $pMessage;
	$LOGGER->err("$prefix $message");
   }
}

function logwarn($pSrc, $pMessage=null){
    if (FP_Application_Subsystem_Logger::$enabled){

	$LOGGER = FP_Application_Subsystem_Logger::$logger;

// 	$bt = debug_backtrace();
// 	$btindex = 1;
// 	if (array_key_exists($btindex, $bt)){
// 	    $lineno = isset($bt[$btindex]['line']) ? $bt[$btindex]['line'] : '';
// 	    $file = isset($bt[$btindex]['file']) ? $bt[$btindex]['file'] : '';
// 	    $function = isset($bt[$btindex]['function']) ? $bt[$btindex]['function'] : '';
// 	    $prefix = "$file:$function:$lineno:";
// 	} else
	    $prefix = '';

	$message = (is_null($pMessage)) ? $pSrc : $pMessage;
	$LOGGER->warn("$prefix $message");
    }
}

function loginfo($pSrc, $pMessage=null){
    if (FP_Application_Subsystem_Logger::$enabled){

	$LOGGER = FP_Application_Subsystem_Logger::$logger;

// 	$bt = debug_backtrace();
// 	$btindex = 1;
// 	if (array_key_exists($btindex, $bt)){
// 	    $lineno = isset($bt[$btindex]['line']) ? $bt[$btindex]['line'] : '';
// 	    $file = isset($bt[$btindex]['file']) ? $bt[$btindex]['file'] : '';
// 	    $function = isset($bt[$btindex]['function']) ? $bt[$btindex]['function'] : '';
// 	    $prefix = "$file:$function:$lineno:";
// 	} else
	    $prefix = '';

	$message = (is_null($pMessage)) ? $pSrc : $pMessage;
	$LOGGER->info("$prefix $message");
    }
}

function logdebug($pSrc, $pMessage=null){
    if (FP_Application_Subsystem_Logger::$enabled){

	$LOGGER = FP_Application_Subsystem_Logger::$logger;

// 	$bt = debug_backtrace();
// 	$btindex = 1;
// 	if (array_key_exists($btindex, $bt)){
// 	    $lineno = isset($bt[$btindex]['line']) ? $bt[$btindex]['line'] : '';
// 	    $file = isset($bt[$btindex]['file']) ? $bt[$btindex]['file'] : '';
// 	    $function = isset($bt[$btindex]['function']) ? $bt[$btindex]['function'] : '';
// 	    $prefix = "$file:$function:$lineno:";
// 	} else
	    $prefix = '';

	$message = (is_null($pMessage)) ? $pSrc : $pMessage;
	$LOGGER->debug("$prefix $message");
    }
}

function logexception($pSrc, $pException=null){
    if (FP_Application_Subsystem_Logger::$enabled){

	$x = (is_null($pException)) ? $pSrc : $pException;
	logerr($x->getMessage() . ' - ' . $x->getTraceAsString());
    }
}

?>